package com.chance.cc.crawler.prod.command.job.domain.vm.xcar.trace;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.xcar.XCarAutoCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;


/**
 * @ClassName song
 * @Description TODO
 * @Author ding
 * @Date 2021/8/29 10:49
 * @Version 1.0
 * 爱卡车系文章回溯
 **/
public class XcarSeriesArticleTraceCrawlerScheduleJob extends XCarAutoCommonCrawlerSchedulerJob {

    public static String site = "SN";

    public static void main(String[] args) {
        publishXcarNewsSchedulerJob();
    }

    public static CrawlerJob publishXcarNewsSchedulerJob(){
        CrawlerJob crawlerJob = xcarCwalerJob();


        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("爱卡汽车车系文章回溯10点定时采集");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());

        return crawlerJob;
    }

    /**
     * 爱卡车系页文章追溯采集
     * @return
     */
    public static CrawlerJob xcarCwalerJob() {
        String url = "https://newcar.xcar.com.cn/";
        String site_biz = site + "-trace";//实时
        HttpConfig httpConfig = HttpConfig.me(domain);
        httpConfig.setHttpSite(site);

        CrawlerRequestRecord record = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpConfig(httpConfig)
                .httpUrl(url)
                .domain(domain)
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null))
//                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,site_biz,"queue")))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .build();


        record.tagsCreator().bizTags().addDomain(domain);
        record.tagsCreator().bizTags().addSite(site);
        record.tagsCreator().bizTags().addSiteBiz(site_biz);
        //创建 评论信息的过滤条件
        CrawlerRecord filterCrawlerRecord =  new CrawlerRecord();
        filterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        filterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null));
        //以json转换保证数据传输的完整性
        record.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(filterCrawlerRecord));
        record.tagsCreator().bizTags().addCustomKV("trace","trace");

        //请求队列名
        String requestQueueName = StringUtils.joinWith("-","crawler",domain,site,site_biz,"queue");
        //comment crawler request record
        CrawlerRequestRecord initCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("sina_mp_item_request",turnPageItem)
                .httpUrl("http://"+metaServiceIp+":"+metaServicePort+"/crawler/domain/common/api/v1/"+domain+"/results/queue/init?requestQueueName="+requestQueueName + "&site="+site+"&hourFromNow=24")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
        HttpRequestBody jsonBody = HttpRequestBody.json(JSON.toJSONString(record), "utf-8");
        initCrawlerRecord.getHttpRequest().setMethod(HttpConstant.Method.POST);
        initCrawlerRecord.getHttpRequest().setRequestBody(jsonBody);


        CrawlerJob crawlerJob = CrawlerJob.builder()
                .crawlerJobThreadNumber(10)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),//xcar-SN-trace-realtime
                        StringUtils.joinWith("-", domain, site,"trace", CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                //.fileResultPipeline("kafka","/data/chance_crawler_runner/logs/node/xcar.log",false)
                .kafkaResultPipeline("kafka", kafkaTopciForTraceJob, null)
                .requestRecord(record)
                .supportRecord(initCrawlerRecord)
                .build();

        crawlerJob.getScheduleTags().getCategoryTag().addLabelTag(CrawlerEnum.CrawlerDataType.comment.enumVal());
        return crawlerJob;
    }

}
